package hot_100;

import java.util.*;

/**
 * 题目描述：
 *      给定一个整数数组nums和一个整数目标值target，请在该数组中找出
 *      和为目标值target的那两个整数，并返回他们的数组下标
 *      
 */

public class TwoSum {
    public static void main(String[] args) {
        int[] nums = new int[]{2, 7, 11, 15};
        int target = 9;
        int[] res = twoSum(nums, target);
        for (int n : res){
            System.out.print(n);
            System.out.print("  ");
        }
    }

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }
}



